const readline = require('readline');
//1.Create readline Interface
//2.'process' is a global object which is given by nodejs.
const rl = readline.createInterface({input:process.stdin,output:process.stdout});
let num1 = Math.floor((Math.random()*10)+1);
let num2 = Math.floor((Math.random()*10)+1);
let answer = num1+num2;
//3.
rl.question('What is '+num1+' + '+num2 +'?\n',(userInput)=>{
    if(userInput.trim() == answer){
        console.log('Correct answer');
        rl.close();//4.It would emit close event;
    }else{
        rl.setPrompt('Incorrect response please try again. \n');
        rl.prompt();
        rl.on('line',(userInput)=>{
            if(userInput.trim() == answer){
                rl.close();
            }else{
                rl.setPrompt('Incorrect response please try again. \n');
                rl.prompt();

            }
        });
    }
});
//4.Listen on 'close' method
rl.on('close',()=>{
    console.log('Correct answer!!!')
});